//
//  CommonGroupViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2019/12/9.
//  Copyright © 2019 AAIM. All rights reserved.
//

#import "CommonGroupViewController.h"
#import "ContactsCell.h"
#import "ChatViewController.h"
#import "ContactRequestLogic.h"

@interface CommonGroupViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * commonTableView;
@property (nonatomic,strong) NSMutableArray * dataAry;
@property (nonatomic,strong) MJTableViewNullDataView * nullDataView;

@end

@implementation CommonGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"共同群组";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.commonTableView];
    [self.commonTableView addSubview:self.nullDataView];
    self.gk_navigationBar.gk_navLineHidden = YES;
    [self getCommonGroupData];
}


-(void)getCommonGroupData{
    [ContactRequestLogic requestCommonGroupWithFuuid:self.Fuuid Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"共同群组%@",Dict);
        KWeakSelf;
        if ([Dict[@"code"] integerValue] == 200) {
            if (![CommonTools isNull:Dict key:@"data"]) {
                for (NSDictionary * dic in Dict[@"data"]) {
                    ContactModel * groupModel = [ContactModel mj_objectWithKeyValues:dic];
                    [weakSelf.dataAry addObject:groupModel];
                }
                if (weakSelf.dataAry.count) {
                    self.nullDataView.hidden = YES;
                }else{
                 self.nullDataView.hidden = NO;
                }
                [weakSelf.commonTableView reloadData];
            }else{
                self.nullDataView.hidden = NO;
            }

        }
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataAry.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ContactsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CommonGroupCell" forIndexPath:indexPath] ;
    if (!cell) {
        cell = [[ContactsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CommonGroupCell"];
    }
    cell.cellType=@"group";
    cell.contactor = _dataAry[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ChatViewController * chat = [[ChatViewController alloc]init];
    [self.navigationController pushViewController:chat animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(UITableView *)commonTableView{
    if (!_commonTableView) {
        _commonTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        _commonTableView.delegate = self;
        _commonTableView.dataSource = self;
        [_commonTableView registerNib:[ContactsCell registerNib] forCellReuseIdentifier:@"CommonGroupCell"];
        _commonTableView.tableFooterView = [UIView new];
//        _commonTableView.tableHeaderView = [UIView new];
        _commonTableView.rowHeight = 50.f;
        _commonTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _commonTableView.backgroundColor = KHEXCOLOR(@"#FFFFFF");
    }
    return _commonTableView;
}

- (NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [[NSMutableArray alloc]init];
    }
    return _dataAry;
}
-(MJTableViewNullDataView*)nullDataView {
    if (!_nullDataView) {
        _nullDataView=[[MJTableViewNullDataView alloc]initWithFrame:CGRectMake(0,30, SCREEN_WIDTH, self.commonTableView.frame.size.height-60)];
        _nullDataView.hidden=YES;
        _nullDataView.backgroundColor=[UIColor whiteColor];
    }
    [_nullDataView setCustomViewFrame:@"空状态" message:@"暂时没有共同群组"];
    return _nullDataView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
